export default class htmlUtil {
    static styleName2ClassName(node:any, less:any) {
        console.log('node',node);

        let html = '';
        let matchArr;
        let pattern = /([^>]*)(<([a-z/][-a-z0-9_:.]*)[^>/]*(\/*)>)([^<]*)/g;
        html = node.replace(/<!--[\s\S]*?-->/g, '');  //去除html注释
        html = html.replace(/>\s+([^\s<]*)\s+</g, '>$1<').trim();  //去除html标签间的多余空白
        const el = document.createElement('div');
        el.innerHTML = html;
        this.traversalUsingNodeIterator(el, less);
        return el.innerHTML;
    }

    static traversalUsingNodeIterator(node:any, less:any) {
        if(node.childNodes&&node.childNodes.length){
            for (let i = 0; i < node.childNodes.length; i++) {
                let item = node.childNodes[i];

                if (item && item.getAttribute) {
                    let styleName = item.getAttribute('stylename');
                    if (styleName) {

                        const styleNames = styleName.split(' ');

                        styleNames.forEach((names:any) => {
                            console.log('styleName', item.className.indexOf(less[names]));
                            if (item.className.indexOf(less[names]) === -1) {
                                item.className += ' ' + less[names];
                            }
                        });
                    }
                    item.removeAttribute('stylename');
                }
                this.traversalUsingNodeIterator(item, less);
            }
        }

    }
}
